Day 26-List/Dictionary Comprehension & NATO Alphabet project


Posted by pei_______ on 2022-05-06

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


List Comprehension
new_list = [new_item for item in list]
new_list = [new_item for item in list if test]

# check the iterate numbers in two text files

with open("file1.txt") as file1:
    data1 = file1.read().splitlines()

with open("file2.txt") as file2:
    data2 = file2.read().splitlines()

result = [int(number) for number in data1 if number in data2 ]

print(result)
# simplify the codes in U.S Game which wrote in Day 25

if answer_state == "Exit":
    missing_state = [state for state in data.state if state not in right_answer]

Dictionary Comprehension
new_dict = {new_key:new_value for item in list}
new_dict = {new_key:new_value for (key,value) in dict.items()}
new_dict = {new_key:new_value for (key,value) in dict.items() if test}

# build students' score dictionary and check whether they passed

names = ['Alex', 'Beth','Carol','Dave','Frank']
import random
student_scores = {student:random.randint(1,100) for student in names}

passed_students = {student:score for (student,score) in student_scores.items() if score >= 60}
# split a sentence and count the letter in a word

sentence = "What is the Airspeed Velocity of an Unladen Swallow?"
result ={letter:len(letter) for letter in sentence.split(" ")}
# change the temperature in Celsius to Fahrenheit

weather_c = {
    "Monday": 12,
    "Tuesday": 14,
    "Wednesday": 15,
    "Thursday": 14,
    "Friday": 21,
    "Saturday": 22,
    "Sunday": 24,
}

weather_f = {day:round((temp_c * 9 / 5)+32,1) for (day, temp_c) in weather_c.items() }

print(weather_f)

NATO Alphabet project

# NATO Alphabet project

import pandas

alphabet_data_frame = pandas.read_csv("nato_phonetic_alphabet.csv")
alphabet_dict = {row.letter: row.code for (index, row) in alphabet_data_frame.iterrows()}

user_word = input("Key a word: ").upper()

result_list = [alphabet_dict[letters] for letters in user_word]
print(result_list)

#Python #課堂筆記 #100 Days of Code







Related Posts

【Day04】實戰開始 - 設計版型以及透過Vue框架建立專案

【Day04】實戰開始 - 設計版型以及透過Vue框架建立專案

【單元測試的藝術】Chap 5: 隔離(模擬)框架

【單元測試的藝術】Chap 5: 隔離(模擬)框架

Day 95

Day 95


Comments